<?php
/**
 * 
 * 
 * @author Ruan Carvalho
 */
class AuthorsController extends AppController {

	var $name = 'Authors';
	
	var $helpers = array('Html', 'Form', 'Js' => array('Jquery'), 'Session');
	
	var $components = array('RequestHandler', 'Session');

	/**
	 * 
	 */
	function index() {
		$this->Author->recursive = 0;
		$this->set('authors', $this->paginate());
	}

	function add() {
		if (!empty($this->data)) {
			$this->Author->create();
			if ($this->Author->save($this->data)) {
				$this->Session->setFlash(__('The author has been saved', true), 'success.message');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The author could not be saved. Please, try again.', true), 'error.message');
			}
		}
		$publications = $this->Author->Publication->find('list');
		$this->set(compact('publications'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid author', true), 'error.message');
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Author->save($this->data)) {
				$this->Session->setFlash(__('The author has been saved', true), 'success.message');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The author could not be saved. Please, try again.', true), 'error.message');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Author->read(null, $id);
		}
		$publications = $this->Author->Publication->find('list');
		$this->set(compact('publications'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for author', true), 'error.message');
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Author->delete($id)) {
			$this->Session->setFlash(__('Author deleted', true), 'success.message');
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Author was not deleted', true), 'error.message');
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	 * Exibe todas as informacoes do Autor selecionado, alem das
	 * publicacoes relacionadas a este.
	 * 
	 * @param $id - Codigo do autor que sera visualizado
	 */
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid author', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('author', $this->Author->read(null, $id));
	}
}
?>